<template>
  <el-dialog :visible.sync="showValue" :before-close="cancel">
    <template slot="title">
      {{title}}
    </template>
    <el-form :model="form" ref="transferOpen" :label-width="formLabelWidth">
      <el-form-item
            v-if="item.value !== 0"
            :label="item.title + '：'" v-for="(item,index) in consts.TRANSFER_PLATFORMS" :key="index">
            <el-radio-group size="medium" v-model="form.transfer[item.value]">
              <el-radio :label="true">开启</el-radio>
              <el-radio :label="false">关闭</el-radio>
            </el-radio-group>
          </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="doSave({action: 'saveForm', form: 'transferOpen'})" :loading="loading">申 请
      </el-button>
    </div>
  </el-dialog>
</template>
<script>
import { form } from "@/build";
import {riskTransferOpenApi as saveApi } from "@/services/api/risk";
import {getTransferConfigs} from '@/services/api/transfer'
export default {
  name: "transfer-open-dialog",
  props: ["showValue", "rowData", "userId"],
  mixins: [form],
  data() {
    return {
      title: "开启转账",
      formLabelWidth: "150px",
      form: {
        transfer: []
      }
    };
  },
  methods: {
    cancel() {
      this.$emit("closeDialog");
    }
  },
  mounted() {
     this.bindSave(saveApi, 'saveForm', 'form', {
        beforeSend: data=>{
          data.transferOpen = this.transferOpen
          let userId=[];
          this.userId.map((item,index)=>{
            userId.push(item.userId)
          })
          return {transferOpen:data.transferOpen,userId:userId.join(",")}
        },
        afterSuccess:data=>{
          this.cancel();
          this.$parent.getAll();
        }
      });
      if(this.rowData != undefined){
        const transferArr = []
        _.map(this.rowData.transferOpen,(item)=>{
          transferArr[item.channelId] = item.open
        });
        this.form.transfer= transferArr
      }else{
        this.bindGet(getTransferConfigs, 'form',{
          format: data=>{
            const {transferOpen} = data
            const transferArr = []
            _.map(transferOpen,(item)=>{
              transferArr[item.channelId] = item.open
            });
            data.transfer= transferArr
            delete  data.transferOpen
            return {...this.form,...data}
          }
        });
      }
      this.getAll();
  },
  computed:{
      transferOpen(){
        const arr = [];
        _.map(this.form.transfer,(item,channelId)=>{
          if(!_.isUndefined(item)){
            arr.push({channelId:channelId, open: item})
          }
        });

        return arr
      }
    },
};
</script>

